package cn.weakup.thread.threadlocal;

import java.util.Random;

/**
 * @author Dawn
 * @version 1.0.0
 * @desc
 * @date 2022/5/10
 */
public class ThreadLocalTest {

    static RequestVO requestVO;
    public static void main(String[] args) {
        Thread threadA = new Thread(()->{
            requestVO = new RequestVO(Thread.currentThread().getName());
            System.out.println(RequestVO.currentUser.get().uid + RequestVO.currentUser.get().name);
        });

        Thread threadB = new Thread(()->{
            requestVO = new RequestVO(Thread.currentThread().getName());
            System.out.println(RequestVO.currentUser.get().uid + RequestVO.currentUser.get().name);
        });

        threadA.start();
        threadB.start();

    }


    static class RequestVO {
        private static ThreadLocal<User> currentUser = new ThreadLocal();
        private String threadName;

        public RequestVO(String threadName) {
            this.threadName = threadName;
            currentUser.set(new User(threadName, new Random().nextInt(10)));
        }

    }

    static class User {
        private String name;
        private Integer uid;

        public User(String name, Integer uid) {
            this.name = name;
            this.uid = uid;
        }
    }
}
